package com.lanou3g.servicedemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_start).setOnClickListener(this);
        findViewById(R.id.btn_stop).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                //启动服务
                //和启动Activity类似 我们启动service也需要一个Intent对象
                //最后调用startService(intent)即可启动service
                Intent startIntent = new Intent(this, MyService.class);
                //startService每一次执行都会调用service的onStartCommand的生命周期
                startService(startIntent);
                break;
            case R.id.btn_stop:
                //停止服务
                //也需要一个intent对象,然后调用stopService即可
                Intent stopIntent = new Intent(this, MyService.class);
                stopService(stopIntent);
                break;
        }

    }
}
